package com.dq.mail.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dq.mail.entity.Spot;
import com.dq.mail.entity.vo.PageResult;
import com.dq.mail.service.SpotService;
import com.dq.mail.utils.R;
import com.dq.mail.vo.spot.SpotVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * Spot-API
 *
 * @author Dq
 * @since 2023-03-07 12:56:26
 */
@Api(value = "景点管理",tags = "景点管理")
@RestController
@RequestMapping("spot")
public class SpotController {
    @Autowired
    private SpotService spotService;


    @ApiOperation("获取轮播图")
    @GetMapping("/bannerList")
    public R spotList() {
        List<Spot> list = spotService.list();
        List<String> bannerList = list.stream().map(Spot::getAccommodationImg).limit(4).collect(Collectors.toList());
        return R.ok().data("bannerList", bannerList);
    }


    /**
     * 根据条件分页查询spot
     *
     * @param currentPage params 当前页数
     * @param pageSize    当前页显示大小
     * @return R
     */
    @ApiOperation("分页查询景点信息")
    @PostMapping("/{currentPage}/{pageSize}")
    public R getSpotsList(@PathVariable("currentPage") Long currentPage,
                          @PathVariable("pageSize") Long pageSize,
                          @RequestBody(required = false) Spot spot
    ) {
        Page<Spot> page = new Page<>(currentPage, pageSize);
        PageResult<Spot> pageResult = spotService.querySpots(page, spot);
        return R.ok().data("pageResult", pageResult);
    }

    /**
     * 根据id查询具体景点
     *
     * @param id 景点id
     * @return R对象
     */
    @ApiOperation("根据Id获取景点信息")
    @GetMapping("/{id}")
    public R getSpotById(@PathVariable Long id) {
        Spot spot = spotService.getById(id);
        if (ObjectUtils.isEmpty(spot)) {
            return R.error().message("查无此数据，请确认景点号是否正确");
        }
        return R.ok().data("spot", spot);
    }

    /**
     * TODO 用户点击收藏按钮
     *
     * @param signId id参数
     * @return R统一返回对象
     */
    @ApiOperation("点赞收藏")
    @GetMapping("/sign/{id}")
    public R signSpot(@PathVariable Long signId) {
        return R.ok();
    }
}